﻿<!DOCTYPE html>
<html class="theme theme-white">
<head>
<meta charset="utf-8">
<title>redis学习---哨兵模式</title>
<link href="https://www.zybuluo.com/static/assets/template-theme-white.css" rel="stylesheet" media="screen">
<style type="text/css">

#wmd-preview h1  {
    color: #0077bb; /* 将标题改为蓝色 */
}</style>
</head>
<body class="theme theme-white">
<div id="wmd-preview" class="wmd-preview wmd-preview-full-reader"><div class="md-section-divider"></div><div class="md-section-divider"></div><h1 data-anchor-id="qqyu" id="redis学习-哨兵模式">redis学习---哨兵模式</h1><div class="md-section-divider"></div><h2 data-anchor-id="bjuj" id="目录">目录</h2><ul data-anchor-id="63fv">
<li>简介</li>
<li>作用</li>
<li>架构图</li>
<li>实现</li>
</ul><div class="md-section-divider"></div><h2 data-anchor-id="t72p" id="1-简介">1. 简介：</h2><p data-anchor-id="knbc">redis的主从方式，使得slavof在整个系统中起到了数据的冗余和备份的作用，但是当master服务挂了之后，整个redis服务处于停用的状态，这时需要有服务在几个slavof中选举出一个新的master服务继续对外提供服务，redis并没有提供master的选举机制，而是需要借助一个哨兵来进行监控并选举</p><div class="md-section-divider"></div><h2 data-anchor-id="ivd7" id="21-作用">2.1 作用：</h2><ol data-anchor-id="nvjd">
<li>监控master和slavof是否正常运行</li>
<li>在slavof中选举新的master服务</li>
</ol><div class="md-section-divider"></div><h2 data-anchor-id="ejdn" id="3-架构图">3. 架构图：</h2><div class="md-section-divider"></div><h3 data-anchor-id="up06" id="31-单哨兵模式">3.1 单哨兵模式</h3><p data-anchor-id="1ap2">哨兵时一个独立的进程，使用单个哨兵时候的架构图 <br>
<img src="http://www.chengqj.com/static/images/redis/%E5%8D%95%E6%9C%BA%E5%93%A8%E5%85%B5.png" alt="单个哨兵架构图"></p><div class="md-section-divider"></div><h3 data-anchor-id="tapo" id="32-单哨兵存在问题">3.2 单哨兵存在问题</h3><p data-anchor-id="zwrh">单个哨兵容易导致单点问题，哨兵的可用性解决方式就是多起几个哨兵的方式，一般都是起单数的哨兵数量以保证系统的稳定。此时哨兵不仅会监控master和slavof，还会监控哨兵,这就是哨兵集群</p><div class="md-section-divider"></div><h3 data-anchor-id="jr7p" id="33-哨兵集群">3.3 哨兵集群</h3><p data-anchor-id="7375"><img src="http://www.chengqj.com/static/images/redis/%E5%93%A8%E5%85%B5%E9%9B%86%E7%BE%A4.png" alt="单个哨兵架构图"></p><div class="md-section-divider"></div><h2 data-anchor-id="or1d" id="4-实现">4. 实现</h2><div class="md-section-divider"></div><h3 data-anchor-id="cvjc" id="41-redis-一主两从">4.1 redis 一主两从</h3><table data-anchor-id="2axl" class="table table-striped-white table-bordered">
<thead>
<tr>
 <th>主机</th>
 <th>端口</th>
 <th>作用</th>
</tr>
</thead>
<tbody><tr>
 <td>192.168.127.131</td>
 <td>6379</td>
 <td>主</td>
</tr>
<tr>
 <td>192.168.127.132</td>
 <td>6379</td>
 <td>从</td>
</tr>
<tr>
 <td>192.168.127.133</td>
 <td>6379</td>
 <td>从</td>
</tr>
</tbody></table><div class="md-section-divider"></div><h3 data-anchor-id="v528" id="42-sentinel三台">4.2 sentinel：三台</h3><table data-anchor-id="18ac" class="table table-striped-white table-bordered">
<thead>
<tr>
 <th>主机</th>
 <th>端口</th>
</tr>
</thead>
<tbody><tr>
 <td>192.168.127.131</td>
 <td>26379</td>
</tr>
<tr>
 <td>192.168.127.132</td>
 <td>26379</td>
</tr>
<tr>
 <td>192.168.127.133</td>
 <td>26379</td>
</tr>
</tbody></table><div class="md-section-divider"></div><h3 data-anchor-id="21ck" id="43-配置方式">4.3 配置方式</h3><p data-anchor-id="314p">配置sentinel.conf文件（目前主服务器没有配置密码）</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="lupo"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="pln">port </span><span class="lit">26379</span></code></li><li class="L1"><code class="language-properties"><span class="com">#mymaster 主服务名称自定义 后续配置都有使用</span></code></li><li class="L2"><code class="language-properties"><span class="com">#192.168.127.131 6379当前主master服务器主机ip和端口 #最后的2表示在sentinel集群中只要有两个节点检测到redis主节点出故障就进行切换</span></code></li><li class="L3"><code class="language-properties"><span class="pln">sentinel monitor mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="lit">2</span></code></li><li class="L4"><code class="language-properties"><span class="com">#去除保护模式  这个不配置，多sentinel的时候就是无法选举</span></code></li><li class="L5"><code class="language-properties"><span class="kwd">protected</span><span class="pun">-</span><span class="pln">mode </span><span class="kwd">no</span></code></li><li class="L6"><code class="language-properties"><span class="com">#如果3s内mymaster无响应，则认为mymaster宕机了</span></code></li><li class="L7"><code class="language-properties"><span class="pln">sentinel down</span><span class="pun">-</span><span class="pln">after</span><span class="pun">-</span><span class="pln">milliseconds mymaster </span><span class="lit">3000</span></code></li><li class="L8"><code class="language-properties"><span class="com">#如果10秒后,mysater仍没活过来，则启动failover</span></code></li><li class="L9"><code class="language-properties"><span class="pln">sentinel failover</span><span class="pun">-</span><span class="pln">timeout mymaster </span><span class="lit">10000</span></code></li><li class="L0"><code class="language-properties"><span class="com">#后台运行</span></code></li><li class="L1"><code class="language-properties"><span class="pln">daemonize yes</span></code></li><li class="L2"><code class="language-properties"><span class="com">#配置运行日志地址</span></code></li><li class="L3"><code class="language-properties"><span class="pln">logfile </span><span class="str">"/home/sentinel.log"</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="ombn" id="44-启动">4.4 启动</h3><p data-anchor-id="6ytc"><strong>命令：</strong></p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="5g74"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="pun">.</span><span class="str">/redis-sentinel ../</span><span class="pln">sentinel</span><span class="pun">.</span><span class="pln">conf</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="hlx7" id="45-启动后各文件变化">4.5 启动后各文件变化</h3><p data-anchor-id="1wxr">启动后sentinel会自动去redis主服务读取个从服务器的信息以及sentinel服务器信息 <br>
sentinel.conf文件最后会自动添加如下信息</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="qfok"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="com">#选项指定了在执行故障转移时， 最多可以有多少个从服务器同时对新的主服务器进行同步</span></code></li><li class="L1"><code class="language-properties"><span class="pln">sentinel config</span><span class="pun">-</span><span class="pln">epoch mymaster </span><span class="lit">0</span></code></li><li class="L2"><code class="language-properties"><span class="pln">sentinel leader</span><span class="pun">-</span><span class="pln">epoch mymaster </span><span class="lit">0</span></code></li><li class="L3"><code class="language-properties"><span class="com">#发现的从服务器地址</span></code></li><li class="L4"><code class="language-properties"><span class="pln">sentinel known</span><span class="pun">-</span><span class="pln">slave mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L5"><code class="language-properties"><span class="com">#发现的从服务器地址</span></code></li><li class="L6"><code class="language-properties"><span class="pln">sentinel known</span><span class="pun">-</span><span class="pln">slave mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.133</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L7"><code class="language-properties"><span class="com">#发现的sentinel服务器地址</span></code></li><li class="L8"><code class="language-properties"><span class="pln">sentinel known</span><span class="pun">-</span><span class="pln">sentinel mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">26379</span><span class="pln"> </span><span class="lit">9fd8006394b620e79492953cdd919f75ea5f922c</span></code></li><li class="L9"><code class="language-properties"><span class="com">#发现的sentinel服务器地址</span></code></li><li class="L0"><code class="language-properties"><span class="pln">sentinel known</span><span class="pun">-</span><span class="pln">sentinel mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.133</span><span class="pln"> </span><span class="lit">26379</span><span class="pln"> </span><span class="lit">30c2efd882c5c454fdd8ac409a00a6eda6b154a1</span></code></li><li class="L1"><code class="language-properties"><span class="pln">sentinel current</span><span class="pun">-</span><span class="pln">epoch </span><span class="lit">0</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="dx6k" id="46-sentinel选举日志">4.6 sentinel选举日志</h3><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="zz2u"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="pun">+</span><span class="pln">sdown master mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L1"><code class="language-properties"><span class="pun">+</span><span class="kwd">new</span><span class="pun">-</span><span class="pln">epoch </span><span class="lit">30</span></code></li><li class="L2"><code class="language-properties"><span class="pun">+</span><span class="pln">vote</span><span class="pun">-</span><span class="kwd">for</span><span class="pun">-</span><span class="pln">leader </span><span class="lit">5dd7a844b99e912240805591f43127594425a553</span><span class="pln"> </span><span class="lit">30</span></code></li><li class="L3"><code class="language-properties"><span class="pun">+</span><span class="pln">odown master mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="com">#quorum 3/2</span></code></li><li class="L4"><code class="language-properties"><span class="typ">Next</span><span class="pln"> failover delay</span><span class="pun">:</span><span class="pln"> I will </span><span class="kwd">not</span><span class="pln"> start a failover before </span><span class="typ">Sat</span><span class="pln"> </span><span class="typ">Sep</span><span class="pln"> </span><span class="lit">29</span><span class="pln"> </span><span class="lit">17</span><span class="pun">:</span><span class="lit">46</span><span class="pun">:</span><span class="lit">51</span><span class="pln"> </span><span class="lit">2018</span></code></li><li class="L5"><code class="language-properties"><span class="pun">+</span><span class="pln">config</span><span class="pun">-</span><span class="pln">update</span><span class="pun">-</span><span class="kwd">from</span><span class="pln"> sentinel </span><span class="lit">5dd7a844b99e912240805591f43127594425a553</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">26379</span><span class="pln"> </span><span class="pun">@</span><span class="pln"> mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L6"><code class="language-properties"><span class="pun">+</span><span class="kwd">switch</span><span class="pun">-</span><span class="pln">master mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L7"><code class="language-properties"><span class="pun">+</span><span class="pln">slave slave </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.133</span><span class="pun">:</span><span class="lit">6379</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.133</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="pun">@</span><span class="pln"> mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L8"><code class="language-properties"><span class="pun">+</span><span class="pln">slave slave </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pun">:</span><span class="lit">6379</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="pun">@</span><span class="pln"> mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L9"><code class="language-properties"><span class="pun">+</span><span class="pln">sdown slave </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pun">:</span><span class="lit">6379</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="pun">@</span><span class="pln"> mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.131</span><span class="pln"> </span><span class="lit">6379</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="0vgv" id="47-选举流程">4.7 选举流程</h3><ol data-anchor-id="wmyu">
<li>每个sentinel节点会定期向master节点发送心跳包来判断存活状态</li>
<li>当down-after-milliseconds选项所指定的时间点都没有回复的时候，这个实例会被sentinel标记为主观下线</li>
<li>当足够多的sentinel的节点数&gt;quorum的节点认为master节点进入主观下线状态时，则master会被标记为客观下线</li>
<li>如果sentinel有多个，需要先用raft算法，选出一个leader来做决策</li>
<li>重新选举出新的master</li>
</ol><div class="md-section-divider"></div><h3 data-anchor-id="cqiv" id="48-重新选举后各文件变化">4.8 重新选举后各文件变化</h3><p data-anchor-id="2y0g">当master挂掉之后自动完成选举后（假设新主为132），各redis.conf变化</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="b5n9"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="com">#主redis.conf自动添加</span></code></li><li class="L1"><code class="language-properties"><span class="pln">slaveof </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span></code></li><li class="L2"><code class="language-properties"><span class="com">#从变主redis.conf 自动去除slavefo属性</span></code></li><li class="L3"><code class="language-properties"><span class="com">#令一个从服务器自动修改slaveof为新的主服务器地址</span></code></li><li class="L4"><code class="language-properties"><span class="pln">slaveof </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span></code></li></ol></pre><p data-anchor-id="0ih0">sentinel的sentinel.conf配置变化</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="fx30"><ol class="linenums"><li class="L0"><code class="language-properties"><span class="com">#会自动替换成新的主服务器地址</span></code></li><li class="L1"><code class="language-properties"><span class="pln">sentinel monitor mymaster </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">127.132</span><span class="pln"> </span><span class="lit">6379</span><span class="pln"> </span><span class="lit">2</span></code></li><li class="L2"><code class="language-properties"><span class="com">#会自动增加</span></code></li><li class="L3"><code class="language-properties"><span class="pln">sentinel config</span><span class="pun">-</span><span class="pln">epoch mymaster </span><span class="lit">1</span><span class="pln"> </span></code></li><li class="L4"><code class="language-properties"><span class="com">#会自动增加</span></code></li><li class="L5"><code class="language-properties"><span class="pln">sentinel leader</span><span class="pun">-</span><span class="pln">epoch mymaster </span><span class="lit">1</span><span class="pln"> </span></code></li><li class="L6"><code class="language-properties"><span class="com">#会自动增加</span></code></li><li class="L7"><code class="language-properties"><span class="pln">sentinel current</span><span class="pun">-</span><span class="pln">epoch </span><span class="lit">1</span><span class="pln"> </span></code></li></ol></pre><hr></div>
</body>
</html>